package codeTop;

public class lcr155 {
    class Node {
        public int val;
        public Node left;
        public Node right;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val,Node _left,Node _right) {
            val = _val;
            left = _left;
            right = _right;
        }
    }
    public Node treeToDoublyList(Node root) {
        dfs(root);
        Node realHead = head.right;
        if (realHead != null) {
            realHead.left = p;
            p.right = realHead;
        }
        return realHead;
    }
    Node head = new Node();
    Node p = head;
    void dfs(Node node) {
        if (node == null) return;
        dfs(node.left);
        p.right = node;
        node.left = p;
        p = p.right;
        dfs(node.right);
    }
}
